<template>
  <div class="container">
    <div class="section-title">
      <span>最新报告</span>
    </div>
    <div class="product-list">
      <div class="intro">
        <div class="intro-title">
          <h2>实时洞见，决策未来</h2>
        </div>
        <div class="intro-text">
          <p>我们是专注于贸易数据统计分析及贸易行业分析报告的分享的平台。</p>
          <p>
            在这里您将可以查询到全球各个国家及地区权威专业的进出口贸易统计数据
          </p>
          <p>这里也将为您分享实时专业的贸易分析报告。</p>
        </div>
        <button class="btn more-btn" @click="handleMore">查看更多</button>
      </div>

      <div
        class="product-item"
        v-for="(item, index) in reportList.slice(0, 1)"
        :key="index"
        @click="handleRowClick(item)"
      >
        <div
          class="price-tag"
          :class="{ free: item.price === '免费' || item.price == 0 }"
        >
          {{ item.price == 0 ? "免费" : "￥" + item.price }}
        </div>
        <div class="image-container">
          <img :src="item.coverPic" />

          <div class="image-title">{{ item.title }}</div>
        </div>
        <div class="hover-buttons"></div>
        <div class="product-item-right">
          <p class="title">{{ item.title }}</p>
          <p class="introduce">{{ item.script }}</p>
          <p class="meta">
            <span class="meta-item">
              <el-icon><User /></el-icon>
              {{ item.author }}
            </span>
            <span class="meta-item">
              <el-icon><Calendar /></el-icon>

              {{ dayjs.unix(item.publishTime).format("YYYY-MM-DD") }}
            </span>
            <span v-if="item.sizeNum" class="meta-item">
              <el-icon><Document /></el-icon>
              {{ item.sizeNum }}
            </span>
          </p>

          <div style="display: flex; gap: 16px">
            <button class="btn-view" @click.stop.prevent="handleView(item)">
              查看详情
            </button>
            <button
              class="btn-download"
              @click.stop.prevent="handleDownload(item)"
            >
              下载报告
            </button>
          </div>
        </div>
      </div>
    </div>

    <div class="product-list">
      <div
        class="product-item"
        v-for="(item, index) in reportList.slice(1)"
        :key="index"
        @click="handleRowClick(item)"
      >
        <div
          class="price-tag"
          :class="{ free: item.price === '免费' || item.price == 0 }"
        >
          {{ item.price == 0 ? "免费" : "￥" + item.price }}
        </div>
        <div class="image-container">
          <img :src="item.coverPic" />
          <div class="image-title">{{ item.title }}</div>
        </div>
        <div class="hover-buttons"></div>
        <div class="product-item-right">
          <p class="title">{{ item.title }}</p>
          <p class="introduce">{{ item.script }}</p>
          <p class="meta">
            <span class="meta-item">
              <el-icon><User /></el-icon>
              {{ item.author }}
            </span>
            <span class="meta-item">
              <el-icon><Calendar /></el-icon>
              {{ dayjs.unix(item.publishTime).format("YYYY-MM-DD") }}
            </span>
            <span v-if="item.sizeNum" class="meta-item">
              <el-icon><Document /></el-icon>
              共 {{ item.sizeNum }}页
            </span>
          </p>

          <div style="display: flex; gap: 16px">
            <button class="btn-view" @click.stop.prevent="handleView(item)">
              查看详情
            </button>
            <button
              class="btn-download"
              @click.stop.prevent="handleDownload(item)"
            >
              下载报告
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { reportApi } from '../../api/report.js'
import { download } from '../../api/Download'
import dayjs from 'dayjs'
import { User, Calendar, Document } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
const reportList = ref([])

const handleDownload = async (item) => {
  try {
    const res = await download.downloadReport({ code: item.code })
    if (res.code === 0) {
      // 创建 a 标签触发下载
      const a = document.createElement('a')
      a.href = res.data.userDown.pdfDir
      a.download = ''
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      ElMessage.success('下载开始')
    } else {
      ElMessage.error(res.msg || '下载失败')
    }
  } catch {
    ElMessage.error('下载出错')
  } finally {

  }
}

const handleMore = () => {
  router.push({ name: 'productView' })
}

const handleView = (item) => {

// 检查是否为免费报告
if (item.price === '0') {
    // 解析免费报告详情页的完整URL
    const freeUrl = router.resolve({
      name: 'productDetail',
      params: { code: item.code }
    }).href;
    
    // 在新标签页打开免费报告
    window.open(freeUrl, '_blank');
  } else {
    // 解析付费报告详情页的完整URL
    const paidUrl = router.resolve({
      name: 'productDetailcharge',
      params: { code: item.code }
    }).href;
    
    // 在新标签页打开付费报告
    window.open(paidUrl, '_blank');
  }
}
onMounted(() => {
  reportApi.getSumData().then(res => {
    reportList.value = res.data.listNew

  })
})

</script>

<style lang="scss" scoped>
.container {
  width: 80%;
  margin: 40px auto;
  display: flex;
  flex-direction: column;

  .section-title {
    position: relative;
    display: inline-block;
    font-size: 24px;
    color: #333;
    text-align: center;
    margin: 20px auto; /* 上下 20px，水平自动居中 */

    &::before {
      content: "Latest report";
      position: absolute;
      top: -25px;
      left: 50%;
      transform: translateX(-50%);
      font-size: 50px;
      color: rgba(0, 0, 0, 0.1);
      white-space: nowrap;
      font-weight: 800;
    }
    &::after {
      content: "";
      display: block;
      width: 60px;
      height: 3px;
      background-color: #0080d1;
      margin: 8px auto 0;
    }
    span {
      font-weight: 600;
      font-size: 40px;
      line-height: 100%;
      letter-spacing: 0%;
      text-align: center;
    }
  }
}

.product-list {
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.product-item {
  background: linear-gradient(to right, #fff, #e5f1f9);
  display: flex;
  align-items: center;
  position: relative;
  overflow: hidden;
  margin-bottom: 20px;
  width: 48.5%;
  height: 275px;
  border-radius: 12px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;

  &::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #1e6a9a3d;
    opacity: 0;
    transition: inherit;
    pointer-events: none;
  }

  .image-container {
    position: relative;
    // margin: 20px 30px;

  
    .image-title {
      position: absolute;
      top: 30%;
      left: 47%;
      /* 先把文字中心移到容器中心 */
      transform: translate(-50%, -50%) rotate(-3deg);
      /* 文字样式 */
      font-size: 12px;
      font-weight: bold;
      font-style: italic;
      color: #fff;
      text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
      white-space: nowrap;
      pointer-events: none;
      text-align: center;
      width: 140px;
      white-space: nowrap; /* 防止文本换行 */
      overflow: hidden; /* 隐藏溢出的内容 */
      text-overflow: ellipsis; /* 显示省略号来代表被修剪的文本 */
    }
  }

  .price-tag {
    position: absolute;
    top: 5px;
    right: 5px;
    padding: 4px 10px;
    background-color: #ff9c00;
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    border-radius: 12px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
    z-index: 10;

    &.free {
      background-color: #28a745;
    }
  }

  .meta {
    display: flex;
    gap: 10px;
    margin-top: 12px;
    align-items: center;
    color: #999;

    &-item {
      display: flex;
      align-items: center;
      gap: 6px;
      font-size: 14px;

      .el-icon {
        color: #151618b8;
        font-size: 16px;
        margin-right: 4px;
      }
    }
  }

  /* 修改按钮样式部分 */
  .btn-download,
  .btn-view {
    margin-top: 30px;
    height: 35px;
    width: 110px;
    padding: 10px 25px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    // gap: 8px;

    .el-icon {
      font-size: 16px;
      transition: color 0.2s ease;
    }

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    }
  }

  .btn-download {
    background: #0080d1;
    color: white;

    &:hover {
      background: #0080d1;
    }

    .el-icon {
      color: rgba(white, 0.9);
    }
  }

  .btn-view {
    background: rgba(white, 0.95);
    color: #151618b8;
    border: 1px solid rgba(#151618b8, 0.2);
  }

  img {
    display: block;
    margin: 20px 30px;
    width: 150px;
    height: 220px;
    object-fit: cover;
   
  }

  .product-item-right {
    margin: auto 10px;
    padding: 15px;
    margin-bottom: 70px;
    .title {
      color: #333;
      font-weight: 600;
      font-size: 20px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1; /* 显示两行 */
      overflow: hidden;
    }
    .introduce {
      font-size: 14px;
      font-weight: 600;
      color: #999;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
    }
    .meta {
      font-size: 12px;
      color: #999;
    }
  }
}
.intro {
  // flex: 0 0 46%;

  padding: 24px;
}
.intro-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 16px;
}

.intro-title .icon {
  width: 24px;
  height: 24px;
  color: #0080d1;
}

.intro-text {
  color: #666;
  line-height: 1.6;
  margin-bottom: 24px;
}

.more-btn {
  background-color: #0080d1;
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}
</style>